package com.lh.gomoku.clientUI;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;

public class MessageReceiver implements Runnable{
	ArrayList<ChessEventListener> listeners;
	boolean goon = true;
	public MessageReceiver(){
		listeners = new ArrayList<ChessEventListener>();
	}
	
	
	//public 
	public void run() {
		BufferedReader br = ClientMain.getScoketReader();
		while(true && goon){
			try {
				if(ClientMain.getSocket().getInputStream().available()!=0){
					String response = br.readLine();
					ChessEvent event = null;
					if(response.startsWith("7000")){
						String[] res = response.split(":");
						String action = res[1];
						if(action.equals("draw")){
							String[] xy = res[2].split(";");
							event = new ChessEvent(ChessEvent.EVENTTYPE_DRAW,xy);
						}else if(action.equals("underpiece")){
							event = ChessEvent.underPieceEvent;
						}else if(action.equals("win")){
							if(res[2].startsWith("GIVEUP")){
								event = new ChessEvent(ChessEvent.EVENTTYPE_WIN,null);
							}else{
								String[] xy = res[2].split(";");
								event = new ChessEvent(ChessEvent.EVENTTYPE_WIN,xy);
							}
						}else if(action.equals("lose")){
							String[] xy = res[2].split(";");
							event = new ChessEvent(ChessEvent.EVENTTYPE_LOSE,xy);
						}else if(action.equals("wait")){
							event = ChessEvent.waitEvent;
						}else if(action.equals("waitanddraw")){
							String[] xy = res[2].split(";");
							event = new ChessEvent(ChessEvent.EVENTTYPE_WAITANDDRAW,xy);
						}else if(action.equals("joinplayer")){
							String[] name = {res[2]};
							event = new ChessEvent(ChessEvent.EVENTTYPE_JOININPLAYER,name);
						}else if(action.equals("playerlist")){
							String[] name = res[2].split(";");
							event = new ChessEvent(ChessEvent.EVENTTYPE_PLAYERLIST,name);
						}else if(action.equals("sueforpeace")){
							String[] name = {res[2]};
							event = new ChessEvent(ChessEvent.EVENTTYPE_SUEPEACE,name);
						}else if(action.equals("sueforpeaceack")){
							String[] yeso = {res[2]};
							event = new ChessEvent(ChessEvent.EVENTTYPE_SUEPEACEACK,yeso);
						}
						else{
							event = new ChessEvent(ChessEvent.EVENTTYPE_RETRY,null);
						}
						
						for(int i=0;i<listeners.size();i++){
							final ChessEventListener listener = listeners.get(i);
							final ChessEvent tmpEvent = event;
							ChessBoardUI.display.syncExec(
								 new Runnable() {
									 public void run(){
										 listener.processChessEvent(tmpEvent);
									 }
								 }
							);
							
						}
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	public void registerListener (ChessEventListener listener){
		if(!listeners.contains(listener)){
			listeners.add(listener);
		}
	}
	public void unregisterListener(ChessEventListener listener){
		if(listeners.contains(listener)){
			listeners.remove(listener);
		}
	}
	public void quite(){
		goon = false;
	}
}
